package com.neuedu.boot.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.boot.entity.Department;
import com.neuedu.boot.framework.Res;
import com.neuedu.boot.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目：      springboot
 * 类名:       DeptController
 * 创建时间：  2024/3/29 14：46
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@CrossOrigin
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    IDeptService deptService;


    /**
     * http://localhost:8080/dept/list
     * @param dept
     * @return
     */
    @RequestMapping("/list")
    public Res<List<Department>> list(Department dept) {

        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        return Res.ok(deptService.list(queryWrapper));
    }


    @RequestMapping("/page")
    public Res<Page>  page(Page<Department> page ,Department dept) {

        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if(dept.getName()!= null && !"".equals(dept.getName().trim())){
            queryWrapper.like("name" ,dept.getName() );
        }
        if(dept.getLeader()!= null && !"".equals(dept.getLeader().trim())){
            queryWrapper.like("leader" ,dept.getLeader() );
        }

        return Res.ok(deptService.page(page ,queryWrapper));
    }

    @RequestMapping("/saveOrUpdate")
    public Res  saveOrUpdate( @RequestBody Department dept){
        return  Res.ok(deptService.saveOrUpdate(dept));
    }


    @RequestMapping("/removeById/{id}")
    public Res  removeById(@PathVariable  int id){
        return  Res.ok(deptService.removeById(id));
    }


    @RequestMapping("/getById/{id}")
    public Res<Department> getById(@PathVariable  int id){
        return  Res.ok(deptService.getById(id));
    }
}
